﻿using CaseManagerLibrary.Licence;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDLicence
{
    public partial class FrmLicence : Form
    {
        public LicenceInfo Licence { get; set; }

        public FrmLicence()
        {
            InitializeComponent();
        }

        public void SetLicence(LicenceInfo licence)
        {
            Licence = licence;
            this.txtClient.Text = licence.Client;
            this.cmbProduct.SelectedItem = licence.ProductId;
            this.txtSN.Text = licence.SN;
            this.dtpStart.Value = Convert.ToDateTime(licence.StartTime);
            this.dtpEnd.Value = Convert.ToDateTime(licence.EndTime);
            this.txtKey.Text = licence.Key;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (Licence == null)
            {
                Licence = new LicenceInfo();
            }

            Licence.Client = txtClient.Text.Trim();
            Licence.ProductId = cmbProduct.SelectedItem.ToString();
            Licence.SN = txtSN.Text.Trim();
            Licence.StartTime = dtpStart.Value.ToString("yyyy-MM-dd");
            Licence.EndTime = dtpEnd.Value.ToString("yyyy-MM-dd");
            Licence.Key = txtKey.Text.Trim();

            try
            {
                string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "licence");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                LicenceManager.CreateLicenceFile(Licence, System.IO.Path.Combine(path, "licence_" + txtSN.Text.Trim() + ".pal"));
            }
            catch (Exception ex)
            { }

            DialogResult = DialogResult.OK;
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnKey_Click(object sender, EventArgs e)
        {
            this.txtKey.Text = Guid.NewGuid().ToString();
        }
    }
}
